-- ==== BEGIN LICENSE BLOCK ====
-- storyboard-addon - Copyright (C) Arnaud Charnoz, 2006-2010.
-- Distributed under the terms of the GNU Lesser General Public License (LGPL) as
-- published by the Free Software Foundation.
-- ===== END LICENSE BLOCK ====

if sb == nil
then
    sb = {};
    if sb.ui == nil
    then
       sb.ui = {}; 
    end
end

sb.ui.StorylineDBManagerController = {};
sb.ui.StorylineDBManagerController.storylineDB = nil;
sb.ui.StorylineDBManagerController.selectedStoryline = nil;

sb.ui.StorylineDBManagerController.selectionIndex = 0;
sb.ui.StorylineDBManagerController.selectedStorylineKey = "Unknow";
sb.ui.StorylineDBManagerController.dropDownMenuVector = {};

-------------------------------------------------------------------------------

function sb.ui.StorylineDBManagerController:init()

    sb_ui_StorylineDBManagerFrame_Title:SetText("Gestionnaire de scénari");
    
end

-------------------------------------------------------------------------------

function sb.ui.StorylineDBManagerController:setStorylineDB( newStorylineDB )

    self.storylineDB = newStorylineDB;
    
end

-------------------------------------------------------------------------------

function sb.ui.StorylineDBManagerController:setSelectedStoryline( newSelectedStoryline )

    self.selectedStoryline = newSelectedStoryline;
    
end

-------------------------------------------------------------------------------

function sb.ui.StorylineDBManagerController:Show()

    sb_ui_StorylineDBManagerFrame:Show();
    
end

-------------------------------------------------------------------------------

function sb.ui.StorylineDBManagerController:IsVisible()

    return sb_ui_StorylineDBManagerFrame:IsVisible();
    
end

-------------------------------------------------------------------------------

function sb.ui.StorylineDBManagerController:Hide()

    sb_ui_StorylineDBManagerFrame:Hide();
    
end

-------------------------------------------------------------------------------

function sb.ui.StorylineDBManagerController:onLoadButton()
    
    newStoryline = self.storylineDB:getStoryline( self.selectedStorylineKey ); 
    
    self.selectedStoryline:sharedCopy( newStoryline ); 
    
    StoryBoard.db.profile.savedStoryline = newStoryline:getId();
    
end

-------------------------------------------------------------------------------

function sb.ui.StorylineDBManagerController:DropDownMenuHandler()

    UIDropDownMenu_SetSelectedID( sb_ui_StorylineDBManagerFrame_SelectorButton, this:GetID() );
    
    self.selectionIndex = UIDropDownMenu_GetSelectedID( sb_ui_StorylineDBManagerFrame_SelectorButton );
    
    self.selectedStorylineKey = self.dropDownMenuVector[ self.selectionIndex ];
     
end

-------------------------------------------------------------------------------

function sb.ui.StorylineDBManagerController_Handler()
    sb.ui.StorylineDBManagerController:DropDownMenuHandler();
end

-------------------------------------------------------------------------------

function sb.ui.StorylineDBManagerController_DropDownMenuOnLoad()
    sb.ui.StorylineDBManagerController:DropDownMenuOnLoad();
end

-------------------------------------------------------------------------------

function sb.ui.StorylineDBManagerController:DropDownMenuOnLoad()

    self.dropDownMenuVector = {}

    local info = {};
    info.func = sb.ui.StorylineDBManagerController_Handler;
    
    if ( sb.ui.StorylineDBManagerController.storylineDB ~= nil )
    then
        local cmpt = 0;
        for k, v in pairs( sb.ui.StorylineDBManagerController.storylineDB.db ) do
            info.text = k;
            cmpt = cmpt + 1;
            info.checked = ( self.selectionIndex == cmpt );
            UIDropDownMenu_AddButton( info );
            table.insert( self.dropDownMenuVector, k );
        end
    end
    
end

-------------------------------------------------------------------------------

function sb.ui.StorylineDBManagerController:onEraseButton()
    
    self.storylineDB:removeStoryline( self.selectedStorylineKey );
    
    sb.ui.StorylineDBManagerController:DropDownMenuOnLoad();
    
    self.selectionIndex = 1;
    self.selectedStorylineKey = self.dropDownMenuVector[ self.selectionIndex ];
    
    UIDropDownMenu_SetSelectedID( sb_ui_StorylineDBManagerFrame_SelectorButton, self.selectionIndex );
    UIDropDownMenu_SetText( sb_ui_StorylineDBManagerFrame_SelectorButton, self.selectedStorylineKey );

end

-------------------------------------------------------------------------------

function sb.ui.StorylineDBManagerController:ShareStoryline()

    local target = sb_ui_StorylineDBManagerFrame_shareValue:GetText();
    if ( target ~= nil )
    then
        --StoryBoard:SendCommMessage("WHISPER", "Here is a message");
        --StoryBoard:SendCommMessage(prefix, text, distribution, target, prio, callbackFn, callbackArg)
        local prefix = StoryBoard.SHARE_STORYLINE_EVENT;
        local text = self.storylineDB:getStringStoryline( self.selectedStorylineKey );
        local distribution = "WHISPER";
        
        StoryBoard:SendCommMessage(prefix, text, distribution, target );
        
    end
    
end

-------------------------------------------------------------------------------

function sb.ui.StorylineDBManagerController:ExportStoryline()

    local dataString = self.selectedStoryline:exportToString();  
   
    if not ExportFrame:IsVisible() then
       ExportFrame:Show();
    end
                       
    ExportFrame_ExportValue:SetText( dataString );

end

-------------------------------------------------------------------------------

function sb.ui.StorylineDBManagerController:ImportStoryline()

    local dataString = ImportFrame_ImportValue:GetText();
   
    self.selectedStoryline:importFromString( dataString );  
    self.storylineDB:addStoryline( self.selectedStoryline );
   
    if ImportFrame:IsVisible() then
       ImportFrame:Hide();
    end
end


-------------------------------------------------------------------------------

